import matplotlib.pyplot as plt
from keras.models import load_model
import numpy as np
from keras.preprocessing.image import ImageDataGenerator, img_to_array,load_img
import tensorflow
import os

#载入模型
model=load_model('model/data/model4_2_VGG 16_cats_vs_dogs_1.h5')
model.summary()
#

def predict(image_path):
    image = load_img(image_path)
    plt.imshow(image)
    plt.show()
    image = image.resize((150, 150))
    image = img_to_array(image)
    image = image / 255
    image = np.expand_dims(image, axis=0)
    # print(image.shape)
    pred = model.predict(image)
    print(pred)
    pred= [(int)((pred[i][0] + 0.5) / 1.0) for i in range(len(pred))]
    print(pred)



if __name__ == "__main__":
    predict('./train_dataset/train/test/cats/cat.1503.jpg')  # 0
    predict('./train_dataset/train/test/dogs/dog.1500.jpg')  # 1
    predict('./train_dataset/train/test/dogs/dog.1504.jpg')  # 1
    predict('./train_dataset/train/test/cats/cat.1509.jpg')  #0

    # 导入图片
# image = load_img('DataSet/test1/1.jpg')
# plt.imshow(image)
# plt.show()
# image = image.resize((150, 150))
# image = img_to_array(image)
# image = image / 255
# image = np.expand_dims(image, axis=0)
# print(image.shape)
# pred = model.predict(image)
# print(pred)
# pred = [(int)((pred[i][0] + 0.5) / 1.0) for i in range(len(pred))]
# print(pred)
# if pred == 0:
#     print("猫")
# else:
#     print("狗")
#
# # print(((pred)>0.5).astype("int32"))
# # pred = np.argmax(pred,axis=1)
#
# image2 = load_img('DataSet/test1/37.jpg')
# plt.imshow(image2)
# plt.show()
# image2 = image2.resize((150, 150))
# image2 = img_to_array(image2)
# image2 = image2 / 255
# image2 = np.expand_dims(image2, axis=0)
# pred2 = model.predict(image2)
# print(pred2)
# pred2 = [(int)((pred2[i][0] + 0.5) / 1.0) for i in range(len(pred2))]
# print(pred2)
# print((pred2 >0.5).astype("int32"))
# print(pred2)